package com.yixun.lbx.controller.farm;

import com.yixun.controller.BaseController;
import com.yixun.lbx.entity.YxProCompany;
import com.yixun.lbx.entity.YxProProductYield;
import com.yixun.lbx.service.CompanyService;
import com.yixun.lbx.service.ProductYieldService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/py")
public class ProductYieldController extends BaseController {
    private final ProductYieldService productYieldService;
    private final CompanyService companyService;

    public ProductYieldController(ProductYieldService productYieldService, CompanyService companyService) {
        this.productYieldService = productYieldService;
        this.companyService = companyService;
    }

    /*
     *产品信息列表
     */
    @PostMapping("/getListByPage")
    public HashMap<String, Object> getListByPage(@RequestBody HashMap<String, Object> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", qyId);
        }
        Integer count = productYieldService.getCount(map);
        List<YxProProductYield> productYields = productYieldService.getListByPage(map);

        map.put("count", count);
        map.put("productYields", productYields);
        return map;
    }

    /*
     *添加产品信息
     */
    @PostMapping("/addProductYield")
    public int addProductYield(@RequestBody HashMap<String, Object> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", qyId);
        }
        map.put("addUser", this.getSessionUser().getUser_id());
        map.put("qyCode", this.getSessionUser().getUser_name());
        return productYieldService.addProductYield(map);
    }

    /*
     *修改产品信息
     */
    @PostMapping("/updateProductYield")
    public int updateProductYield(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        return productYieldService.updateProductYield(map);
    }

    /*
     *删除产品信息
     */
    @PostMapping("/deleteProductYield")
    public int deleteProductYield(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        String[] ids = map.get("id").toString().split(",");
        map.put("ids", ids);
        return productYieldService.deleteProductYield(map);
    }

    @GetMapping("/getObjByArgs")
    public List<YxProProductYield> getObjByArgs(YxProProductYield proProductYield) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            proProductYield.setQyId(String.valueOf(qyId));
        }
        return productYieldService.getObjByArgs(proProductYield);
    }

    private Long getQyId() {
        YxProCompany yxProCompany = new YxProCompany();
        yxProCompany.setCode(this.getSessionUser().getUser_name());
        YxProCompany company = companyService.getCompanyByIdOrNameOrCode(yxProCompany);
        if (company != null) {
            return company.getQyId();
        } else {
            return 0L;
        }
    }


    /*搜索*/
    @RequestMapping("/getSearchListByPage")
    public HashMap<String, Object> getSearchListByPage(@RequestBody HashMap<String, String> map) {
        HashMap<String, Object> hashMap = new HashMap<>();
        map.put("qyId", String.valueOf(this.getQyId()));
        List<YxProProductYield> companies = productYieldService.getSearchListByPage(map);
        hashMap.put("companies", companies);
        hashMap.put("count", companies.size());
        return hashMap;
    }
}
